<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备标识" prop="equipmentid">
        <el-input
          v-model="queryParams.equipmentid"
          placeholder="请输入mqtt服务设备标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentname">
        <el-input
          v-model="queryParams.equipmentname"
          placeholder="请输入mqtt服务设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP地址" prop="equipmentip">
        <el-input
          v-model="queryParams.equipmentip"
          placeholder="请输入mqtt服务设备IP"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="端口号" prop="port">
        <el-input
          v-model="queryParams.port"
          placeholder="请输入mqqt服务端口"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备位置" prop="devicelocation">
        <el-input
          v-model="queryParams.devicelocation"
          placeholder="请输入设备位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mqtt:equipmentinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mqtt:equipmentinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mqtt:equipmentinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mqtt:equipmentinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" v-if="false" />
      <el-table-column label="mqtt服务设备标识" align="center" prop="equipmentid" />
      <el-table-column label="mqtt服务设备名称" align="center" prop="equipmentname" />
       <!-- 展开列 -->
      <el-table-column type="expand">
        <template #default="props">
          <el-row :gutter="10" class="mb8">        
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-open" size="mini" @click="handleitmeClick(props.row)">查询订阅数据</el-button>        
            </el-col>  
            <el-col :span="1.5"> <el-button size="mini" type="text"  icon="el-icon-monitor"
                    @click="handleAdditem(props.row)"  >添加订阅</el-button> 
            </el-col>
          </el-row>
          <el-table
              v-if="props.row.equipclient && props.row.equipclient.length > 0"
              :data="props.row.equipclient"
              border
            > <el-table-column label="服务器设备标识ID" align="center" prop="equipmentid" />
              <el-table-column label="别称" align="center" prop="anothername" />
              <el-table-column label="Client ID" align="center" prop="clientId" />
              <el-table-column label="用户名" align="center" prop="loginname" />
              <el-table-column label="密码" align="center" prop="passworld" />
             
            </el-table>
        </template>
      </el-table-column>
      <el-table-column label="mqtt服务设备IP" align="center" prop="equipmentip" />
      <el-table-column label="mqqt服务端口" align="center" prop="port" />
      <el-table-column label="设备位置" align="center" prop="devicelocation" />
      <el-table-column label="设备补充说明" align="center" prop="description" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="最近数据获得时间" align="center" prop="lasttimeupdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lasttimeupdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mqtt:equipmentinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mqtt:equipmentinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="设备标识" prop="equipmentid">
          <el-input v-model="form.equipmentid" placeholder="请输入mqtt服务设备标识" />
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentname">
          <el-input v-model="form.equipmentname" placeholder="请输入mqtt服务设备名称" />
        </el-form-item>
        <el-form-item label="设备IP" prop="equipmentip">
          <el-input v-model="form.equipmentip" placeholder="请输入mqtt服务设备IP" />
        </el-form-item>
        <el-form-item label="端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入mqqt服务端口" />
        </el-form-item>
        <el-form-item label="设备位置" prop="devicelocation">
          <el-input v-model="form.devicelocation" placeholder="请输入设备位置" />
        </el-form-item>
        <el-form-item label="设备补充说明" prop="description">
          <el-input v-model="form.description" placeholder="请输入设备补充说明" />
        </el-form-item>
        <el-form-item label="最近数据获得时间" prop="lasttimeupdate">
          <el-date-picker clearable
            v-model="form.lasttimeupdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最近数据获得时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible" :title="title"  width="500px" height="200px"
      custom-class="custom-dialog"
        :show-close="true"
      :destroy-on-close="true"> 
        <ECDetail :equipID="selectedItem" :equipclientID="selectedItemID" :key="dialogKey"/>
    </el-dialog>
  </div>
</template>

<script>
import { listEquipmentinfo, getEquipmentinfo, delEquipmentinfo, addEquipmentinfo, updateEquipmentinfo } from "@/api/mqtt/equipmentinfo";
import { listEquipclient, getEquipclient, getECinfoByEquipmentId,delEquipclient, addEquipclient, updateEquipclient } from "@/api/mqtt/equipclient";
import ECDetail from '@/views/mqtt/equipclient/index.vue';

export default {
  name: "Equipmentinfo",
  components: { ECDetail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      equipmentinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentid: null,
        equipmentname: null,
        equipmentip: null,
        port: null,
        devicelocation: null,
        description: null,
        status: null,
        lasttimeupdate: null
      },
      // 表单参数
      form: {},
      dialogKey: 0, // 添加一个key变量
      selectedItem:null,
      selectedItemID:null,
      dialogVisible: false,
      // 表单校验
      rules: {
        equipmentid: [
          { required: true, message: "mqtt服务设备标识不能为空", trigger: "blur" }
        ],
        equipmentname: [
          { required: true, message: "mqtt服务设备名称不能为空", trigger: "blur" }
        ],
        equipmentip: [
          { required: true, message: "mqtt服务设备IP不能为空", trigger: "blur" }
        ],
        port: [
          { required: true, message: "mqqt服务端口不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listEquipmentinfo(this.queryParams).then(response => {
        this.equipmentinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentid: null,
        equipmentname: null,
        equipmentip: null,
        port: null,
        devicelocation: null,
        description: null,
        status: null,
        lasttimeupdate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加MQTT服务设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEquipmentinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改MQTT服务设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEquipmentinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipmentinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delEquipmentinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mqtt/equipmentinfo/export', {
        ...this.queryParams
      }, `equipmentinfo_${new Date().getTime()}.xlsx`)
    },
    handleitmeClick(row) { 
      // 模拟从后台查询传感器数据
      getECinfoByEquipmentId(row.equipmentid).then(response => {   
        row.equipclient = response.data; 
      });
    },
    /** 新增订阅按钮操作 */
    handleAdditem(row) {  
      this.selectedItem=row.equipmentid; 
      this.title =row.equipmentip+ "——添加订阅";
      this.dialogKey += 1; // 每次打开对话框时增加key值以强制重新渲染
      this.dialogVisible = true;
    },
    /** 修改订阅按钮操作 */
    handleUpdateitem(row) {
      this.reset(); 
      const id = row.id || this.ids
      this.selectedItemID=id;
      this.title = "修改订阅"+this.selectedItemI;
      this.dialogKey += 1; // 每次打开对话框时增加key值以强制重新渲染
      this.dialogVisible = true;
    }
  }
};
</script>
